<?php
header("Content-type: text/html; charset=utf-8");
defined('EBusinessID') or define('EBusinessID', 1260548);
defined('AppKey') or define('AppKey', 'dfdd56ba-b2a6-45dd-bcdb-b9565ab2b18b');
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx');
class KdniaoAction extends Action
{
    function getExpress($name,$Express,$orderNum)
    {
        $requestData = "{'OrderCode':'','ShipperCode':'$Express','LogisticCode':'$orderNum'}";
        $datas = array(
            'EBusinessID' => EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
        );
        $datas['DataSign'] = self::encrypt($requestData, AppKey);
        $result = self::sendPost(ReqURL, $datas);
        $result = json_decode($result,true);
//       State 2-在途中,3-签收,4-问题件
        $result['name']="快递方式：".$name;
        $result['LogisticCode']="运 单 号：". $result['LogisticCode'];
        $result['Traces'] = array_reverse($result['Traces']);
        switch($result['State']){
            case 2:
                $result['State']= '状 态：在途中';
                break;
            case 3:
                $result['State']= '状 态：签收';
                break;
            case 4:
                $result['State']= '状 态：问题件';
                break;
        }
        return $result;
    }

    function getOrderTracesByXml()
    {
        $requestData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" .
            "<Content>" .
            "<OrderCode></OrderCode>" .
            "<ShipperCode>SF</ShipperCode>" .
            "<LogisticCode>589707398027</LogisticCode>" .
            "</Content>";

        $datas = array(
            'EBusinessID' => EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData),
            'DataType' => '1',
        );
        $datas['DataSign'] = self::encrypt($requestData, AppKey);
        $result = self::sendPost(ReqURL, $datas);

        return $result;
    }

    function sendPost($url, $datas)
    {
        $temps = array();
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);
        }
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
        if ($url_info['port'] == '') {
            $url_info['port'] = 80;
        }
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader .= "Host:" . $url_info['host'] . "\r\n";
        $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader .= "Connection:close\r\n\r\n";
        $httpheader .= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
        $headerFlag = true;
        while (!feof($fd)) {
            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
                break;
            }
        }
        while (!feof($fd)) {
            $gets .= fread($fd, 128);
        }
        fclose($fd);

        return $gets;
    }

    function encrypt($data, $appkey)
    {
        return urlencode(base64_encode(md5($data . $appkey)));
    }
}
?>